package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customerManager.CustomerPoolRuleMapper;
import com.crm.model.entity.customerManager.CustomerPoolRule;
import com.crm.model.entity.customerManager.CustomerPoolRuleExample;
import com.crm.service.customerManager.CustomerPoolRuleDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/17 15:15
 */
@Service
public class CustomerPoolRuleDbServiceImpl implements CustomerPoolRuleDbService {
    @Autowired
    CustomerPoolRuleMapper customerPoolRuleMapper;

    @Override
    public Boolean insertCustomerPoolRule(CustomerPoolRule customerPoolRule) {
        Boolean result = false;
        int num = customerPoolRuleMapper.insertSelective(customerPoolRule);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public Boolean deleteCustomerPoolRuleByCluePoolId(String customerPoolId) {
        CustomerPoolRuleExample customerPoolRuleExample = new CustomerPoolRuleExample();
        CustomerPoolRuleExample.Criteria criteria = customerPoolRuleExample.createCriteria();
        criteria.andCustomerPoolIdEqualTo(customerPoolId);
        CustomerPoolRule customerPoolRule = new CustomerPoolRule();
        customerPoolRule.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return customerPoolRuleMapper.updateByExampleSelective(customerPoolRule, customerPoolRuleExample) > 0;
    }

    @Override
    public List<CustomerPoolRule> selectCustomerPoolRuleByCluePoolId(String customerPoolId) {
        CustomerPoolRuleExample customerPoolRuleExample = new CustomerPoolRuleExample();
        CustomerPoolRuleExample.Criteria criteria = customerPoolRuleExample.createCriteria();
        criteria.andCustomerPoolIdEqualTo(customerPoolId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return customerPoolRuleMapper.selectByExample(customerPoolRuleExample);
    }

    /**
     * 根据客户池id查询客户池领取规则
     * @param customerPoolId
     * @return
     */
    @Override
    public CustomerPoolRule selectCustomerPoolRuleReceive(String customerPoolId) {
        CustomerPoolRuleExample customerPoolRuleExample = new CustomerPoolRuleExample();
        CustomerPoolRuleExample.Criteria criteria = customerPoolRuleExample.createCriteria();
        criteria.andCustomerPoolIdEqualTo(customerPoolId);
        criteria.andRuleTypeEqualTo(1);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerPoolRule> customerPoolRules = customerPoolRuleMapper.selectByExample(customerPoolRuleExample);
        if (customerPoolRules != null && !customerPoolRules.isEmpty()){
            return customerPoolRules.get(0);
        }
        return null;
    }
}
